{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    },
    "colab": {
      "name": "02 - Embedding Songs to Make Recommendations.ipynb",
      "provenance": [],
      "include_colab_link": true
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/jalammar/jalammar.github.io/blob/master/notebooks/nlp/02_Song_Embeddings.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Qn0kiKeiJzR3",
        "colab_type": "text"
      },
      "source": [
        "# 02 - Song Embeddings - Skipgram Recommender\n",
        "\n",
        "In this notebook, we'll use human-made music playlists to learn song embeddings. We'll treat a playlist as if it's a sentence and the songs it contains as words. We feed that to the word2vec algorithm which then learns embeddings for every song we have. These embeddings can then be used to recommend similar songs. This technique is used by Spotify, AirBnB, Alibaba, and others. It accounts for a vast portion of their user activity, user media consumption, and/or sales (in the case of Alibaba).\n",
        "\n",
        "The [dataset we'll use](https://www.cs.cornell.edu/~shuochen/lme/data_page.html) was collected by Shuo Chen from Cornell University. The dataset contains playlists from hundreds of radio stations from around the US."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nLJjlV_MJzR4",
        "colab_type": "text"
      },
      "source": [
        "## Importing packages and dataset"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DjCkD_O4JzR5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import gensim \n",
        "from gensim.models import Word2Vec\n",
        "from urllib import request\n",
        "import warnings\n",
        "warnings.filterwarnings('ignore')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8o8IWI5HJzR9",
        "colab_type": "text"
      },
      "source": [
        "The playlist dataset is a text file where every line represents a playlist. That playlist is basically a series of song IDs. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o2qaaX6SJzR_",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Get the playlist dataset file\n",
        "data = request.urlopen('https://storage.googleapis.com/maps-premium/dataset/yes_complete/train.txt')\n",
        "\n",
        "# Parse the playlist dataset file. Skip the first two lines as \n",
        "# they only contain metadata\n",
        "lines = data.read().decode(\"utf-8\").split('\\n')[2:] \n",
        "\n",
        "# Remove playlists with only one song\n",
        "playlists = [s.rstrip().split() for s in lines if len(s.split()) > 1]\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RXQsINptJzSD",
        "colab_type": "text"
      },
      "source": [
        "The `playlists` variable now contains a python list. Each item in this list is a playlist containing song ids. We can look at the first two playlists here:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "scrolled": false,
        "id": "suXLSoj5JzSU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 122
        },
        "outputId": "c9ad7ca8-83a5-4895-c2dd-559cf5cae5c7"
      },
      "source": [
        "print( 'Playlist #1:\\n ', playlists[0], '\\n')\n",
        "print( 'Playlist #2:\\n ', playlists[1])"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Playlist #1:\n",
            "  ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '2', '42', '43', '44', '45', '46', '47', '48', '20', '49', '8', '50', '51', '52', '53', '54', '55', '56', '57', '25', '58', '59', '60', '61', '62', '3', '63', '64', '65', '66', '46', '47', '67', '2', '48', '68', '69', '70', '57', '50', '71', '72', '53', '73', '25', '74', '59', '20', '46', '75', '76', '77', '59', '20', '43'] \n",
            "\n",
            "Playlist #2:\n",
            "  ['78', '79', '80', '3', '62', '81', '14', '82', '48', '83', '84', '17', '85', '86', '87', '88', '74', '89', '90', '91', '4', '73', '62', '92', '17', '53', '59', '93', '94', '51', '50', '27', '95', '48', '96', '97', '98', '99', '100', '57', '101', '102', '25', '103', '3', '104', '105', '106', '107', '47', '108', '109', '110', '111', '112', '113', '25', '63', '62', '114', '115', '84', '116', '117', '118', '119', '120', '121', '122', '123', '50', '70', '71', '124', '17', '85', '14', '82', '48', '125', '47', '46', '72', '53', '25', '73', '4', '126', '59', '74', '20', '43', '127', '128', '129', '13', '82', '48', '130', '131', '132', '133', '134', '135', '136', '137', '59', '46', '138', '43', '20', '139', '140', '73', '57', '70', '141', '3', '1', '74', '142', '143', '144', '145', '48', '13', '25', '146', '50', '147', '126', '59', '20', '148', '149', '150', '151', '152', '56', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '60', '176', '51', '177', '178', '179', '180', '181', '182', '183', '184', '185', '57', '186', '187', '188', '189', '190', '191', '46', '192', '193', '194', '195', '196', '197', '198', '25', '199', '200', '49', '201', '100', '202', '203', '204', '205', '206', '207', '32', '208', '209', '210']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3ShdD5-lJzSZ",
        "colab_type": "text"
      },
      "source": [
        "## Training the Word2Vec Model\n",
        "Our dataset is now in the shape the the Word2Vec model expects as input. We pass the dataset to the model, and set the following key parameters:\n",
        " * **size**: Embedding size for the songs. \n",
        " * **window**: word2vec algorithm parameter -- maximum distance between the current and predicted word (song) within a sentence\n",
        " * **negative**: word2vec algorithm parameter -- Number of negative examples to use at each training step that the model needs to identify as noise\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rEfuTFyFJzSZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = Word2Vec(playlists, size=32, window=20, negative=50, min_count=1, workers=4)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "n0tqsF2zJzSc",
        "colab_type": "text"
      },
      "source": [
        "The model is now trained. Every song has an embedding. We only have song IDs, though, no titles or other info. Let's grab the song information file."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fCfBAMz0JzSh",
        "colab_type": "text"
      },
      "source": [
        "## Song Title and Artist File\n",
        "Let's load and parse the file containing song titles and artists"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QFphp8SKJzSh",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "songs_file = request.urlopen('https://storage.googleapis.com/maps-premium/dataset/yes_complete/song_hash.txt')\n",
        "songs_file = songs_file.read().decode(\"utf-8\").split('\\n')\n",
        "songs = [s.rstrip().split('\\t') for s in songs_file]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jx0Iu6wgJzSk",
        "colab_type": "text"
      },
      "source": [
        "Now, `songs` is a list containing the id, title, and artist of every song in our datset. It looks like this:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pSSxd6CTJzSk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "8074f5d3-1465-4d40-ee04-6e623a144ebe"
      },
      "source": [
        "songs[:3]"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[['0 ', 'Gucci Time (w\\\\/ Swizz Beatz)', 'Gucci Mane'],\n",
              " ['1 ', 'Aston Martin Music (w\\\\/ Drake & Chrisette Michelle)', 'Rick Ross'],\n",
              " ['2 ', 'Get Back Up (w\\\\/ Chris Brown)', 'T.I.']]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wQwC1_y4JzSn",
        "colab_type": "text"
      },
      "source": [
        "To simplify looking up song titles by ID, we'll define a pandas dataframe to hold song information."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M1iUpILxJzSo",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "songs_df = pd.DataFrame(data=songs, columns = ['id', 'title', 'artist'])\n",
        "songs_df = songs_df.set_index('id')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fTCf3UEwJzSr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "9cb9f0ff-331d-4cd1-f216-aea254aa702e"
      },
      "source": [
        "songs_df.head()"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>Gucci Time (w\\/ Swizz Beatz)</td>\n",
              "      <td>Gucci Mane</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Aston Martin Music (w\\/ Drake &amp; Chrisette Mich...</td>\n",
              "      <td>Rick Ross</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Get Back Up (w\\/ Chris Brown)</td>\n",
              "      <td>T.I.</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Hot Toddy (w\\/ Jay-Z &amp; Ester Dean)</td>\n",
              "      <td>Usher</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Whip My Hair</td>\n",
              "      <td>Willow</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                                title      artist\n",
              "id                                                               \n",
              "0                        Gucci Time (w\\/ Swizz Beatz)  Gucci Mane\n",
              "1   Aston Martin Music (w\\/ Drake & Chrisette Mich...   Rick Ross\n",
              "2                       Get Back Up (w\\/ Chris Brown)        T.I.\n",
              "3                  Hot Toddy (w\\/ Jay-Z & Ester Dean)       Usher\n",
              "4                                        Whip My Hair      Willow"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "g5Jw-y1WJzSy",
        "colab_type": "text"
      },
      "source": [
        "Pandas dataframes give us the ability to easily search through the columns of our dataset. We can look at the songs of a certain artist, for example."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "scrolled": true,
        "id": "IuaxAA7-JzS3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "97a6bff7-03f7-4c2e-8bed-1d578509507d"
      },
      "source": [
        "songs_df[songs_df.artist == 'Rush'].head()"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>1861</th>\n",
              "      <td>Tom Sawyer</td>\n",
              "      <td>Rush</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2640</th>\n",
              "      <td>Red Barchetta</td>\n",
              "      <td>Rush</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2655</th>\n",
              "      <td>Fly By Night</td>\n",
              "      <td>Rush</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2691</th>\n",
              "      <td>Freewill</td>\n",
              "      <td>Rush</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2748</th>\n",
              "      <td>Limelight</td>\n",
              "      <td>Rush</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "               title artist\n",
              "id                         \n",
              "1861      Tom Sawyer   Rush\n",
              "2640   Red Barchetta   Rush\n",
              "2655    Fly By Night   Rush\n",
              "2691        Freewill   Rush\n",
              "2748       Limelight   Rush"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8CcB_zlwJzS6",
        "colab_type": "text"
      },
      "source": [
        "### Looking up songs by their IDs\n",
        "Pandas also give us the ability to retrieve the information of multiple songs by passing their ids. Let's for example retrieve the info for songs number 1, 10, and 100."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3B4DxzDbJzS7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 173
        },
        "outputId": "3bf9a25d-0835-48b1-d770-b90f93d5aae3"
      },
      "source": [
        "songs_df.iloc[[1,10,100]]"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>Aston Martin Music (w\\/ Drake &amp; Chrisette Mich...</td>\n",
              "      <td>Rick Ross</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>10</th>\n",
              "      <td>Shake It</td>\n",
              "      <td>Elephant Man</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>100</th>\n",
              "      <td>I'm Yours</td>\n",
              "      <td>Jason Mraz</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                                  title        artist\n",
              "id                                                                   \n",
              "1     Aston Martin Music (w\\/ Drake & Chrisette Mich...     Rick Ross\n",
              "10                                             Shake It  Elephant Man\n",
              "100                                           I'm Yours    Jason Mraz"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YEgExcykJzS-",
        "colab_type": "text"
      },
      "source": [
        "## Recommending Similar Songs\n",
        "Let's now pick a song, and see what similar songs the model recommends"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5_n6cAoEJzS_",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "5ecfa18c-21d1-4797-cd08-8a5805e708cd"
      },
      "source": [
        "songs_df.iloc[2172]"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "title     Fade To Black\n",
              "artist        Metallica\n",
              "Name: 2172 , dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CfeCwtSWJzTC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        },
        "outputId": "26272a47-38e6-46ca-dd0a-b14057ca37c8"
      },
      "source": [
        "song_id = 2172\n",
        "\n",
        "# Ask the model for songs similar to song #2172\n",
        "model.wv.most_similar(positive=str(song_id))"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[('3094', 0.9966088533401489),\n",
              " ('3116', 0.9964900612831116),\n",
              " ('3167', 0.9961047172546387),\n",
              " ('9994', 0.9953345060348511),\n",
              " ('6624', 0.99521803855896),\n",
              " ('2976', 0.9952161908149719),\n",
              " ('3148', 0.9950860738754272),\n",
              " ('2849', 0.9950583577156067),\n",
              " ('2886', 0.9945998191833496),\n",
              " ('2715', 0.9944301247596741)]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Z-K9IcajJzTF",
        "colab_type": "text"
      },
      "source": [
        "Let's look up the titles and artists of these songs:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NNY1IUS1JzTG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 390
        },
        "outputId": "237bd097-82d4-4ec6-9354-348e4b4194ce"
      },
      "source": [
        "similar_songs = np.array(model.wv.most_similar(positive=str(song_id)))[:,0]\n",
        "songs_df.iloc[similar_songs]"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>3094</th>\n",
              "      <td>Breaking The Law</td>\n",
              "      <td>Judas Priest</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3116</th>\n",
              "      <td>Communication Breakdown</td>\n",
              "      <td>Led Zeppelin</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3167</th>\n",
              "      <td>Unchained</td>\n",
              "      <td>Van Halen</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9994</th>\n",
              "      <td>Atomic Punk</td>\n",
              "      <td>Van Halen</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6624</th>\n",
              "      <td>Everybody Wants Some!!!</td>\n",
              "      <td>Van Halen</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2976</th>\n",
              "      <td>I Don't Know</td>\n",
              "      <td>Ozzy Osbourne</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3148</th>\n",
              "      <td>Big City Nights</td>\n",
              "      <td>Scorpions</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2849</th>\n",
              "      <td>Run To The Hills</td>\n",
              "      <td>Iron Maiden</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2886</th>\n",
              "      <td>The Zoo</td>\n",
              "      <td>Scorpions</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2715</th>\n",
              "      <td>Rainbow In The Dark</td>\n",
              "      <td>Dio</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                         title         artist\n",
              "id                                           \n",
              "3094          Breaking The Law   Judas Priest\n",
              "3116   Communication Breakdown   Led Zeppelin\n",
              "3167                 Unchained      Van Halen\n",
              "9994               Atomic Punk      Van Halen\n",
              "6624   Everybody Wants Some!!!      Van Halen\n",
              "2976              I Don't Know  Ozzy Osbourne\n",
              "3148           Big City Nights      Scorpions\n",
              "2849          Run To The Hills    Iron Maiden\n",
              "2886                   The Zoo      Scorpions\n",
              "2715       Rainbow In The Dark            Dio"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "t7Ubu5OUJzTI",
        "colab_type": "text"
      },
      "source": [
        "Let's define a function that prints out both the song title and the recommendations based on it:\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Fk9rEnlqJzTJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def print_recommendations(song_id):\n",
        "    print( songs_df.iloc[song_id] )\n",
        "    similar_songs = np.array(model.wv.most_similar(positive=str(song_id)))[:,0]\n",
        "    return  songs_df.iloc[similar_songs] \n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k2R6D_g9JzTL",
        "colab_type": "text"
      },
      "source": [
        "## More Example Recommendations\n",
        "\n",
        "### Paranoid Android - Radiohead"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oX7Y6eKLJzTM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 441
        },
        "outputId": "9d8d84f6-2a58-47dd-ff9f-bae6e14e723d"
      },
      "source": [
        "print_recommendations(19563)"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "title     Paranoid Android\n",
            "artist           Radiohead\n",
            "Name: 19563 , dtype: object\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>22889</th>\n",
              "      <td>Pictures Of Matchstick Men</td>\n",
              "      <td>Camper Van Beethoven</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>21628</th>\n",
              "      <td>I Got Mine</td>\n",
              "      <td>The Black Keys</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>38877</th>\n",
              "      <td>No New Tale To Tell</td>\n",
              "      <td>Love And Rockets</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>40337</th>\n",
              "      <td>Optimistic</td>\n",
              "      <td>Radiohead</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>56561</th>\n",
              "      <td>Pass The Mic</td>\n",
              "      <td>The Beastie Boys</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>62660</th>\n",
              "      <td>I Don't Believe You</td>\n",
              "      <td>The Thermals</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>40382</th>\n",
              "      <td>The Boy With The Thorn In His Side</td>\n",
              "      <td>The Smiths</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>19207</th>\n",
              "      <td>Bro Hymn</td>\n",
              "      <td>Pennywise</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>41910</th>\n",
              "      <td>Sister Havana</td>\n",
              "      <td>Urge Overkill</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1740</th>\n",
              "      <td>Lights</td>\n",
              "      <td>Interpol</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                     title                artist\n",
              "id                                                              \n",
              "22889           Pictures Of Matchstick Men  Camper Van Beethoven\n",
              "21628                           I Got Mine        The Black Keys\n",
              "38877                  No New Tale To Tell      Love And Rockets\n",
              "40337                           Optimistic             Radiohead\n",
              "56561                         Pass The Mic      The Beastie Boys\n",
              "62660                  I Don't Believe You          The Thermals\n",
              "40382   The Boy With The Thorn In His Side            The Smiths\n",
              "19207                             Bro Hymn             Pennywise\n",
              "41910                        Sister Havana         Urge Overkill\n",
              "1740                                Lights              Interpol"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zDTdwsUnJzTU",
        "colab_type": "text"
      },
      "source": [
        "### California Love - 2Pac"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gsSRrnv2JzTW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 441
        },
        "outputId": "7a2c7bd9-a02d-464d-87a4-3cc806586f41"
      },
      "source": [
        "print_recommendations(842)"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "title     California Love (w\\/ Dr. Dre & Roger Troutman)\n",
            "artist                                              2Pac\n",
            "Name: 842 , dtype: object\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>27081</th>\n",
              "      <td>Give Me Everything (w\\/ Ne-Yo, Afrojack &amp; Nayer)</td>\n",
              "      <td>Pitbull</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>34678</th>\n",
              "      <td>Run De Riddim</td>\n",
              "      <td>3 Canal</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9666</th>\n",
              "      <td>Gimme The Light</td>\n",
              "      <td>Sean Paul</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5788</th>\n",
              "      <td>Drop It Like It's Hot (w\\/ Pharrell)</td>\n",
              "      <td>Snoop Dogg</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>22255</th>\n",
              "      <td>Kiss Kiss (w\\/ T-Pain)</td>\n",
              "      <td>Chris Brown</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5792</th>\n",
              "      <td>Rock That Body</td>\n",
              "      <td>Black Eyed Peas</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>330</th>\n",
              "      <td>Hate It Or Love It (w\\/ 50 Cent)</td>\n",
              "      <td>The Game</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5668</th>\n",
              "      <td>How We Do (w\\/ 50 Cent)</td>\n",
              "      <td>The Game</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5696</th>\n",
              "      <td>Carry Out (w\\/ Justin Timberlake)</td>\n",
              "      <td>Timbaland</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>20048</th>\n",
              "      <td>Back Seat (w\\/ Dev)</td>\n",
              "      <td>New Boyz</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                                   title           artist\n",
              "id                                                                       \n",
              "27081   Give Me Everything (w\\/ Ne-Yo, Afrojack & Nayer)          Pitbull\n",
              "34678                                      Run De Riddim          3 Canal\n",
              "9666                                     Gimme The Light        Sean Paul\n",
              "5788                Drop It Like It's Hot (w\\/ Pharrell)       Snoop Dogg\n",
              "22255                             Kiss Kiss (w\\/ T-Pain)      Chris Brown\n",
              "5792                                      Rock That Body  Black Eyed Peas\n",
              "330                     Hate It Or Love It (w\\/ 50 Cent)         The Game\n",
              "5668                             How We Do (w\\/ 50 Cent)         The Game\n",
              "5696                   Carry Out (w\\/ Justin Timberlake)        Timbaland\n",
              "20048                                Back Seat (w\\/ Dev)         New Boyz"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NiQe6W-IJzTY",
        "colab_type": "text"
      },
      "source": [
        "### Billie Jean - Michael Jackson"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Qa6uAAuxJzTZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 441
        },
        "outputId": "407b2db8-94d3-4081-e98f-dd95f2e22402"
      },
      "source": [
        "print_recommendations(3822)"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "title         Billie Jean\n",
            "artist    Michael Jackson\n",
            "Name: 3822 , dtype: object\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>title</th>\n",
              "      <th>artist</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>id</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>12749</th>\n",
              "      <td>Wanna Be Startin' Somethin'</td>\n",
              "      <td>Michael Jackson</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4181</th>\n",
              "      <td>Kiss</td>\n",
              "      <td>Prince &amp; The Revolution</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4187</th>\n",
              "      <td>I Wanna Dance With Somebody (Who Loves Me)</td>\n",
              "      <td>Whitney Houston</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1506</th>\n",
              "      <td>The Way You Make Me Feel</td>\n",
              "      <td>Michael Jackson</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3358</th>\n",
              "      <td>Maneater</td>\n",
              "      <td>Daryl Hall &amp; John Oates</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3924</th>\n",
              "      <td>Broken Wings</td>\n",
              "      <td>Mr. Mister</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3357</th>\n",
              "      <td>Manic Monday</td>\n",
              "      <td>The Bangles</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>15660</th>\n",
              "      <td>Let The Music Play</td>\n",
              "      <td>Shannon</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>500</th>\n",
              "      <td>Don't Stop 'Til You Get Enough</td>\n",
              "      <td>Michael Jackson</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3809</th>\n",
              "      <td>Super Freak</td>\n",
              "      <td>Rick James</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                             title                   artist\n",
              "id                                                                         \n",
              "12749                  Wanna Be Startin' Somethin'          Michael Jackson\n",
              "4181                                          Kiss  Prince & The Revolution\n",
              "4187    I Wanna Dance With Somebody (Who Loves Me)          Whitney Houston\n",
              "1506                      The Way You Make Me Feel          Michael Jackson\n",
              "3358                                      Maneater  Daryl Hall & John Oates\n",
              "3924                                  Broken Wings               Mr. Mister\n",
              "3357                                  Manic Monday              The Bangles\n",
              "15660                           Let The Music Play                  Shannon\n",
              "500                 Don't Stop 'Til You Get Enough          Michael Jackson\n",
              "3809                                   Super Freak               Rick James"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KKDGWsAZKqh-",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}